import request from '@/axios'
import { check, checkResultPage, Page, ResultPage } from "./baseModel"
import { Arrayable } from 'element-plus/es/utils'
import { FormItemRule } from 'element-plus'

export interface RiderSearch {
    name: string | undefined
    phone: string | undefined
    status: boolean | undefined
}

export interface Rider {
    id: string
    name: string
    phone: string
    joinTime: string
    leaveTime: string
    totalOrders: number
    todayOrders: number
    status: boolean
}

export interface RiderForm {
    id: string
    name: string
    phone: string
    status: boolean
}

export const riderFormRules: Partial<Record<string, Arrayable<FormItemRule>>> = {
    name: [
        { required: true, message: '请输入骑手姓名', trigger: 'blur' },
        { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
    ],
    phone: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
    ]
}

export function getInitRiderSearch() {
    return {
        name: undefined,
        phone: undefined,
        status: undefined
    }
}

export function getInitRiderForm() {
    return {
        id: '',
        name: '',
        phone: '',
        status: true
    }
}

/**
 * 用于发起商品请求的 API
 *  static getAll()                         获取所有激活的骑手数据
 *  static pagingSearch(search, page)       分页获取数据
 *  static create(Form)                     新增数据
 *  static deleteById(id)                   删除数据
 *  static updateById(Form, id)             修改数据
 *  addTags(form)                           新增标签 
 *  deleteTags(id, tags)                    删除标签
 */
export class RiderApi {

    static baseUrl = '/rider'

    // 获取所有激活的骑手数据
    static async getAll(): Promise<Rider[]> {
        try {
            const response = await request.get(this.baseUrl, {data: {loading: '加载数据'}})
            return check(response) as Rider[]
        } 
        catch (error: any) { throw error }
    }

    // 分页获取数据
    static async pagingSearch(search: RiderSearch, page: Page): Promise<ResultPage<Rider>> {
        try {
            const response = await request.get(
                `${this.baseUrl}/search`,
                { params: { ...search, ...page }, data: { loading: '加载商品数据' } }
            )
            return checkResultPage(response) as ResultPage<Rider>
        }
        catch (error: any) { throw error }
    }

    // 新增数据
    static async create(form: RiderForm): Promise<Rider> {
        try {
            const response = await request.post(
                this.baseUrl,
                {...form, loading: '新增骑手'}
            )
            return check(response) as Rider;
        } 
        catch (error: any) { throw error }
    }

    // 删除数据
    static async deleteById(id: string) {
        try {
            const response = await request.delete(
                `${this.baseUrl}/${id}`,
                {data: {loading: '删除商品中'}}
            )
            check(response)
        }
        catch (error: any) { throw error }
    }

    // 修改数据
    static async updateById(form: RiderForm, id: string): Promise<Rider> {
        try {
            const response = await request.put(
                `${this.baseUrl}/${id}`,
                {...form, loading: '修改数据'}
            )
            return check(response) as Rider
        } 
        catch (error: any) { throw error }
    }

}